package com.originskyseed.platform.count.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.count.mapper.TeamAchievementMapper;
import com.originskyseed.platform.count.model.TeamAchievement;
import com.originskyseed.platform.count.service.ITeamAchievementService;
import com.originskyseed.platform.user.mapper.DeptMapper;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.Dept;

/**
 * 小组业绩信息表Service接口实现类（OSI_TEAM_ACHIEVEMENT）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("teamAchievementService")
public class TeamAchievementServiceImpl implements ITeamAchievementService {

	@Resource
	private TeamAchievementMapper teamAchievementMapper;

	@Resource
	private DeptMapper deptMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.teamAchievementMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.teamAchievementMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(TeamAchievement record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.teamAchievementMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(TeamAchievement record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.teamAchievementMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(TeamAchievement record) throws Exception {
		record.setUpdateTime(new Date());
		return this.teamAchievementMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(TeamAchievement record) throws Exception {
		record.setUpdateTime(new Date());
		return this.teamAchievementMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public TeamAchievement selectByPrimaryKey(String id) throws Exception {
		return this.teamAchievementMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<TeamAchievement> selectList(TeamAchievement record) throws Exception {
		return this.teamAchievementMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<TeamAchievement> selectListByPage(Map<String, Object> params, BasePage<TeamAchievement> page) throws Exception {
		params.put("page", page);
		List<TeamAchievement> list = this.teamAchievementMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据小组每日业绩数据
	 * 
	 * @param date
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	public int insertsTeamAchievementByTime(String date, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		// 要新增的集合
		List<TeamAchievement> teamAchievementList = new ArrayList<>();
		// 查询出所有的小组。
		Dept queryDept = new Dept();
		queryDept.setType(5);
		queryDept.setSourceSys(2);
		List<Dept> deptList = this.deptMapper.selectList(queryDept);
		for (Dept dept : deptList) {
			List<String> userIds = this.userInfoMapper.selectUserIdsByDeptId(dept.getId());
			if (userIds.size() > 0) {
				Map<String, Object> map = new HashMap<>();
				map.put("list", userIds);
				map.put("date", date);
				TeamAchievement teamAchievement = this.teamAchievementMapper.selectTeamAchievementByTime(map);
				teamAchievement.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				teamAchievement.setDate(date);
				teamAchievement.setDeptId(dept.getId());
				teamAchievement.setName(dept.getName());
				teamAchievement.setCreateTime(new Date());
				teamAchievement.setSevenLoginBonus(BigDecimal.ZERO);
				teamAchievement.setThirtyDayLogin(0);
				teamAchievement.setThirtyLoginBonus(BigDecimal.ZERO);
				teamAchievementList.add(teamAchievement);
			} else {
				TeamAchievement teamAchievement = new TeamAchievement();
				teamAchievement.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
				teamAchievement.setDate(date);
				teamAchievement.setDeptId(dept.getId());
				teamAchievement.setName(dept.getName());
				teamAchievement.setCreateTime(new Date());
				teamAchievement.setRegCount(0);
				teamAchievement.setValidRegCount(0);
				teamAchievement.setIpRegCount(0);
				teamAchievement.setOneDayLogin(0);
				teamAchievement.setSecondDayLogin(0);
				teamAchievement.setThreeDayLogin(0);
				teamAchievement.setFourDayLogin(0);
				teamAchievement.setFiveDayLogin(0);
				teamAchievement.setSixthDayLogin(0);
				teamAchievement.setSevenDayLogin(0);
				teamAchievement.setSevenLoginBonus(BigDecimal.ZERO);
				teamAchievement.setThirtyDayLogin(0);
				teamAchievement.setThirtyLoginBonus(BigDecimal.ZERO);
				teamAchievementList.add(teamAchievement);
			}
		}

		int count = 0;
		// 一次性插入的数量
		int size = 500;
		// 总长度
		int length = teamAchievementList.size();
		// 计算可以分成多少组
		int num = (length + size - 1) / size;
		for (int i = 0; i < num; i++) {
			// 开始位置
			int fromIndex = i * size;
			// 结束位置
			int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
			List<TeamAchievement> newTeamAchievementList = teamAchievementList.subList(fromIndex, toIndex);
			count += teamAchievementMapper.insertBatch(newTeamAchievementList);
		}
		return count;
	}
}